package com.gaohe.web.controller.dcp;

import com.gaohe.common.annotation.DataScope;
import com.gaohe.common.annotation.Log;
import com.gaohe.common.core.controller.BaseController;
import com.gaohe.common.core.domain.AjaxResult;
import com.gaohe.common.core.domain.entity.SysUser;
import com.gaohe.common.core.domain.model.LoginUser;
import com.gaohe.common.core.page.TableDataInfo;
import com.gaohe.common.enums.BusinessType;
import com.gaohe.common.utils.SecurityUtils;
import com.gaohe.common.utils.poi.ExcelUtil;
import com.gaohe.dcp.domain.DcpSign;
import com.gaohe.dcp.domain.vo.DcpSignVo;
import com.gaohe.dcp.service.IDcpSignService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户签名Controller
 *
 * @author ruoyi
 * @date 2025-05-14
 */
@RestController
@RequestMapping("/dcp/sign")
public class DcpSignController extends BaseController
{
    @Autowired
    private IDcpSignService dcpSignService;

    /**
     * 查询用户签名列表
     */
//    @DataScope(userAlias = "p")
    @PreAuthorize("@ss.hasPermi('dcp:sign:list')")
    @GetMapping("/list")
    public TableDataInfo list(DcpSignVo dcpSign) {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        // 根据角色设置查询条件
        if (!SecurityUtils.isAdmin(user.getUserId()))
        {
            // 检查是否为医院负责人（部门管理员）
            if (user.getRoles().stream().anyMatch(r -> "director".equals(r.getRoleKey())))
            {
            }
            else
            {
                // 普通医生只能查看自己的排班
                dcpSign.setUserId(user.getUserId());
            }
        }

        startPage();
        List<DcpSignVo> list = dcpSignService.selectDcpSignList(dcpSign);
        return getDataTable(list);
    }

    /**
     * 导出用户签名列表
     */
    @PreAuthorize("@ss.hasPermi('dcp:sign:export')")
    @Log(title = "用户签名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DcpSignVo dcpSign)
    {
        List<DcpSignVo> list = dcpSignService.selectDcpSignList(dcpSign);
        ExcelUtil<DcpSignVo> util = new ExcelUtil<DcpSignVo>(DcpSignVo.class);
        util.exportExcel(response, list, "用户签名数据");
    }

    /**
     * 获取用户签名详细信息
     */
    @PreAuthorize("@ss.hasPermi('dcp:sign:query')")
    @GetMapping(value = "/{signId}")
    public AjaxResult getInfo(@PathVariable("signId") Long signId)
    {
        return success(dcpSignService.selectDcpSignBySignId(signId));
    }

    /**
     * 新增用户签名
     */
    @PreAuthorize("@ss.hasPermi('dcp:sign:add')")
    @Log(title = "用户签名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DcpSign dcpSign)
    {
        return toAjax(dcpSignService.insertDcpSign(dcpSign));
    }

    /**
     * 修改用户签名
     */
    @PreAuthorize("@ss.hasPermi('dcp:sign:edit')")
    @Log(title = "用户签名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DcpSign dcpSign)
    {
        return toAjax(dcpSignService.updateDcpSign(dcpSign));
    }

    /**
     * 删除用户签名
     */
    @PreAuthorize("@ss.hasPermi('dcp:sign:remove')")
    @Log(title = "用户签名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{signIds}")
    public AjaxResult remove(@PathVariable Long[] signIds)
    {
        return toAjax(dcpSignService.deleteDcpSignBySignIds(signIds));
    }

    @PreAuthorize("@ss.hasPermi('dcp:sign:list')")
    @GetMapping("/getSignList/{id}")
    public TableDataInfo getSignList(@PathVariable("id") Long userId)
    {
        List<DcpSignVo> list = dcpSignService.getSignList(userId);
        return getDataTable(list);
    }
}
